/**
 * 
 */
package org.cnbi.web.view.xml;

import java.util.ArrayList;
import java.util.List;

import org.cnbi.utils.chart.base.BaseChart;
import org.cnbi.utils.chart.component.Column;
import org.cnbi.utils.chart.json.JsonSeriesChart;
import org.cnbi.utils.chart.json.JsonSingleSeries;
import org.cnbi.utils.chart.json.component.JsonApplication;
import org.cnbi.utils.chart.json.component.JsonDefinition;
import org.cnbi.utils.chart.json.component.JsonStyles;
import org.cnbi.utils.chart.xml.XmlSeriesChart;
import org.cnbi.utils.chart.xml.component.Applications;
import org.cnbi.utils.chart.xml.component.Definitions;
import org.cnbi.utils.chart.xml.component.XmlApplication;
import org.cnbi.utils.chart.xml.component.XmlDefinition;
import org.cnbi.utils.chart.xml.component.XmlStyles;
import org.cnbi.utils.dynamic.DynamicBeanUtil;
import org.cnbi.utils.pojo.DynamicBean;

/**
 * Copyright © 2014中国经邦. All rights reserved.
 * 
 * @author 龚佳新
 * @date 2014年4月11日下午5:44:41
 * @cnbi-web
 */
public class ChartUtil {

	public static XmlSeriesChart createXmlSeriesChart() {// String
															// []seriesNames,List<Data>
															// datas,
		BaseChart chart = new BaseChart("测试的");
		chart.setXaxisname("指标公司");
		chart.setYaxisname("值");
		chart.setShowlegend(null);
		List<Column> colList = new ArrayList<Column>();
		Column col1 = new Column("item", "名称");
		Column col2 = new Column("A", "本期", "6E8B3D");// setJSONUrl setJSONData
		Column col3 = new Column("b", "累计", "FF0000");
		colList.add(col1);
		colList.add(col2);
		colList.add(col3);
		List<DynamicBean> list = getList(colList);
		chart.setYaxisvaluedecimals("2");
		XmlSeriesChart bean = new XmlSeriesChart(colList, list);
		bean.setStyles(generaXmlStyle());
		return bean;
	}

	public static JsonSeriesChart createJsonSeriesChart() {
		BaseChart chart = new BaseChart("测试的");
		chart.setXaxisname("指标公司");
		chart.setYaxisname("值");
		chart.setShowlegend(null);
		List<Column> colList = new ArrayList<Column>();
		Column col1 = new Column("item", "名称");
		Column col2 = new Column("A", "本期", "6E8B3D");// setJSONUrl setJSONData
		Column col3 = new Column("b", "累计", "FF0000");
		colList.add(col1);
		colList.add(col2);
		colList.add(col3);
		List<DynamicBean> list = getList(colList);
		chart.setYaxisvaluedecimals("2");
		JsonSeriesChart bean = new JsonSeriesChart(colList, list, chart);
		bean.setStyles(generaJsonStyle());
		return bean;
	}
	
	public static JsonSingleSeries createJsonSingleChart() {
		BaseChart chart = new BaseChart("测试的");
	//	chart.setXaxisname("指标公司");
		//chart.setYaxisname("值");
		//chart.setShowlegend(null);
		List<Column> colList = new ArrayList<Column>();
		Column col1 = new Column("item", "名称");
		Column col2 = new Column("A", "本期", "6E8B3D");// setJSONUrl setJSONData
		colList.add(col1);
		colList.add(col2);
		List<DynamicBean> list = getList(colList);
		JsonSingleSeries bean = new JsonSingleSeries(colList,list,chart);
		bean.setStyles(generaJsonStyle());
		return bean;
	}

	/**
	 * 
	 */
	public static JsonStyles generaJsonStyle() {
		List<JsonDefinition> definitionList = new ArrayList<JsonDefinition>();
		List<JsonApplication> applicationList = new ArrayList<JsonApplication>();
		JsonDefinition definition = new JsonDefinition("样式一");
		JsonApplication application = new JsonApplication();
		application.setStyles(definition.getName());
		definitionList.add(definition);
		applicationList.add(application);
		return new JsonStyles(definitionList, applicationList);
	}

	public static XmlStyles generaXmlStyle() {
		List<XmlDefinition> definitionList = new ArrayList<XmlDefinition>();
		List<XmlApplication> applicationList = new ArrayList<XmlApplication>();
		XmlDefinition definition = new XmlDefinition("样式一");
		XmlApplication application = new XmlApplication();
		application.setStyles(definition.getName());
		definitionList.add(definition);
		applicationList.add(application);
		return new XmlStyles(new Definitions(definitionList), new Applications(applicationList));
	}

	private static List<DynamicBean> getList(List<Column> colList) {
		int colLen = colList.size();
		String label[] = new String[colLen];// {"index0","index1","index2"};
		for (int i = 0; i < colLen; i++) {
			label[i] = colList.get(i).getIndex();
		}
		List<DynamicBean> list = new ArrayList<DynamicBean>();
		for (int j = 0; j < 3; j++) {
			String value[] = new String[label.length];
			for (int i = 0; i < colLen; i++) {
				if (label[i].equals("index0")) {
					value[i] = "公司" + j;
				} else {
					value[i] = (int) (Math.random() * 10) + 1 + "0.54";
				}
			}
			DynamicBean bean = createBean(label, value);
			list.add(bean);
		}
		return list;
	}

	private static DynamicBean createBean(String label[], String value[]) {
		DynamicBean bean = DynamicBeanUtil.getDynamicBean(label, null);
		for (int i = 0, len = label.length; i < len; i++) {
			bean.setValue(label[i], String.valueOf(value[i]));
		}
		return bean;
	}
}
